<?php
namespace app\my\service;
use app\my\model\AttentionModel;
use app\my\validata\MyValidata;
use app\base\service\BaseService;
use think\facade\Log;
//我的类
class AttentionService extends BaseService
{
	protected $mv = '';
	protected $am = '';

	 public function __construct()
	 {
	 	parent::__construct();
	 	$this->mv = new MyValidata;
	 	$this->am = new AttentionModel;
	 }
	//关注用户
	protected function attention_users($data){
		if (!$this->mv->scene('attention_users')->check($data)) {
            return $this->return_code(300,$this->mv->getError());
        }
        //判断用户是否存在
        $user['Openid']=$data['passOpenid'];
         $users = $this->am->countWhere('userinfo',$user); 
         if($users<=0) return $this->return_code(205,"用户不存在");
        $isMuAttention =1;
        if($data['mutualOpenid'] == $data['passOpenid']) return $this->return_code(203,"同一人不能关注");
         //先查询是否是互相关注
        $find['mutualOpenid']=$data['passOpenid'];
        $find['passOpenid']=$data['mutualOpenid'];
        $find['status']=1;
        $finds = $this->am->countWhere('attention',$find); 

        if($finds >0) {
        	$isMuAttention = 2;
        	$up['isMuAttention'] =2;
        	$s = $this->am->updateByWhere('attention',$find,$up);
        }

        $ismu['mutualOpenid']=$data['mutualOpenid'];
        $ismu['passOpenid']=$data['passOpenid'];

        $ismus = $this->am->findByAttributes('attention',$ismu);
       
       
        if($ismus['status'] ==1) {return $this->return_code(201,"重复关注");}
        else if($ismus['status'] ==2){  //取消关注后重新关注
        	$ups['status'] =1;
        	$ups['isMuAttention'] =$isMuAttention;
        	$find2['mutualOpenid']=$data['mutualOpenid'];
        	$find2['passOpenid']=$data['passOpenid'];
        	$find2['status']=2;
        	$res = $this->am->updateByWhere('attention',$find2,$ups);
        
        }else{
        	$in['mutualOpenid']=$data['mutualOpenid'];
	        $in['passOpenid']=$data['passOpenid'];
	        $in['isMuAttention']=$isMuAttention;
	        $in['addTime']=time();
	        $in['status']=1;
        	$res = $this->am->insert('attention',$in);
        	
        }
        if($res) return $this->return_code(200,"关注成功");
        return $this->return_code(202,"关注失败");
	}

	//取消关注
	protected function attention_cancels($data)
	{
		if (!$this->mv->scene('attention_users')->check($data)) {
            return $this->return_code(300,$this->mv->getError());
        }

     $res = $this->am->attention_cancels($data);
     if($res) return $this->return_code(200,"取消成功");

     return $this->return_code(201,"取消失败");
        
	}

	//列表
	protected function lists($data){
		if (!$this->mv->scene('openid')->check($data)) {
            return $this->return_code(300,$this->mv->getError());
        }
        $data['page']=(isset($data['page'])&&!empty($data['page']))?$data['page']:1;

        $res = $this->am->list($data);
     	if($res) return $this->return_code(200,"成功",$res);
     	return $this->return_code(201,"数据为空");

	}

	//是否关注
	protected function isattentions($data)
	{
		$where['mutualOpenid']=$data['mutualOpenid'];
		$where['passOpenid']=$data['passOpenid'];
		$where['status']=1;
		$res = $this->am->countWhere('attention',$where);
		if($res) return $this->return_code(202,"已关注");
     	return $this->return_code(200,"未关注");
	} 


}